﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace ThresholdAssistant.Tools
{
    /// <summary>
    /// AES加密工具类
    /// </summary>
    public class AesEncryptUtil
    {
        /// <summary>
        /// 有密码的AES加密 
        /// </summary>
        /// <param name="text">需要加密的字符</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Encrypt(string text, string key)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(text);
            RijndaelManaged rDel = new RijndaelManaged()
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            cTransform.Dispose();
            return resultArray.Aggregate("", (current, t) => $"{current}{t:x2}");
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Decrypt(string text, string key)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = new byte[text.Length / 2];
            for (int i = 0; i < text.Length; i += 2)
            {
                toEncryptArray[i / 2] = (byte)Convert.ToInt32(text.Substring(i, 2), 16);
            }
            RijndaelManaged rDel = new RijndaelManaged()
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            cTransform.Dispose();
            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// 随机生成16位AESkey
        /// </summary>
        /// <returns></returns>
        public static string GeyRandomAESKey()
        {
            string str = string.Empty;
            Random rnd1 = new Random();
            int r = rnd1.Next(10, 100);
            long num2 = DateTime.Now.Ticks + r;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> r)));
            for (int i = 0; i < 16; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }
    }
}